package com.atlyn.web;

import com.atlyn.bean.Classroom;
import com.atlyn.bean.Plan;
import com.atlyn.bean.Teacher;
import com.atlyn.bean.TeacherClassroomTime;
import com.atlyn.dao.FuncDao;
import com.google.gson.Gson;

import javax.ejb.Local;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TeacherServlet extends BaseServlet {
    FuncDao funcDao = new FuncDao();
    public void SelectAllClassroom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Classroom> classrooms = funcDao.func1();
        request.setAttribute("classrooms",classrooms);
        request.getRequestDispatcher("/ClassroomForTeacher.jsp").forward(request,response);
    }

    public void SelectClassroomById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clnum = request.getParameter("searchmsg");
        Classroom classroom = funcDao.search(clnum);
        if(classroom != null){
            request.setAttribute("classroom",classroom);
        }else {
            request.setAttribute("msg","没有此教室信息！");
        }

        request.getRequestDispatcher("/ClassroomForTeacher.jsp").forward(request,response);
    }
    public void getTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int tid = Integer.parseInt(request.getParameter("tid"));
        Teacher teacher = funcDao.SelectTeacher(tid);
        request.setAttribute("teacher",teacher);
        request.getRequestDispatcher("/edit_teacher.jsp").forward(request,response);
    }
    public void updateTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int tid = Integer.parseInt(request.getParameter("tid"));
        String tname = request.getParameter("tname");
        String course = request.getParameter("course");
        String level = request.getParameter("level");
        String tpwd = request.getParameter("tpwd");
        funcDao.editTeacher(tid,tname,course,level,tpwd);
        Teacher teacher = new Teacher(tid,tname,course,level,tpwd);
        request.setAttribute("teacher",teacher);
        request.getRequestDispatcher("/tHome.jsp").forward(request,response);
    }
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int tid = Integer.parseInt(request.getParameter("tid"));
        Teacher teacher = funcDao.SelectTeacher(tid);
        request.setAttribute("teacher",teacher);
        request.getRequestDispatcher("/tHome.jsp").forward(request,response);
    }
    public void selectCourseNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ajax请求过来了");
        int tid = Integer.parseInt(request.getParameter("tid"));
        List<TeacherClassroomTime> teacherClassroomTimes = funcDao.SelectCourse(tid);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DATE);
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        List<TeacherClassroomTime> list = new ArrayList<TeacherClassroomTime>();
        for (int i = 0;i < teacherClassroomTimes.size();i++){
//        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

            TeacherClassroomTime teacherClassroomTime = teacherClassroomTimes.get(i);
            calendar.setTime(teacherClassroomTime.getStart());
            int year1 = calendar.get(Calendar.YEAR);
            int month1 = calendar.get(Calendar.MONTH);
            int day1 = calendar.get(Calendar.DATE);

            if (year == year1 && month == month1 && day == day1){
                System.out.println(teacherClassroomTime);
                list.add(teacherClassroomTime);
            }
        }
        Gson gson = new Gson();
        System.out.println(gson.toJson(list));
        response.getWriter().write(gson.toJson(list));
    }
    //查询老师的所有课表
    public void SelectAllCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hhh来看课表啦");
        int tid = Integer.parseInt(request.getParameter("tid"));
        List<TeacherClassroomTime> teacherClassroomTimes = funcDao.SelectCourse(tid);
        request.setAttribute("list",teacherClassroomTimes);
        request.getRequestDispatcher("/TeacherPlanAll.jsp").forward(request,response);
    }
}
